package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zyf.vc.ui.PlayVideoActiviy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.componet.MyScrollGridView;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.sign.MarkPhotoActivity;
import cn.acewill.yxd.yunxundian.ui.sign.WrittingEvaluationListActivity;
import cn.acewill.yxd.yunxundian.ui.user.ImageViewPagerActivity;

/**
 * Created by XXX on 2017/11/30.
 */

public class WriteEvaluationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    private Context mContext;
    private List<tempLostRR> rrList;
    private BtnOnClickLisener mbtnOnClickLisener;

    public WriteEvaluationAdapter(Context context,List<tempLostRR> list,BtnOnClickLisener btnOnClickLisener){
        this.mContext = context;
        this.rrList = list;
        this.mbtnOnClickLisener = btnOnClickLisener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return ITEM_TYPE_HEADER;
        }else{
            return ITEM_TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType==ITEM_TYPE_HEADER){
            view=LayoutInflater.from(mContext).inflate(R.layout.write_eva_title_view,parent,false);
            return new HeadHolder(view);
        }else{
            view = LayoutInflater.from(mContext).inflate(R.layout.yxd_add_evaluation,parent,false);
            return new WriteHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {

        if (holder instanceof HeadHolder){ //签名

            ((HeadHolder)holder).jibendata.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mbtnOnClickLisener.jibenClick();
                }
            });
            ((HeadHolder)holder).evaDesc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mbtnOnClickLisener.onDesc();
                }
            });
            ((HeadHolder) holder).addEva.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mbtnOnClickLisener.addEvaClick();
                }
            });
            ((HeadHolder)holder).signature.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mbtnOnClickLisener.goSignature();
                }
            });
            //整改要求
            ((HeadHolder) holder).rectRequest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mbtnOnClickLisener.feedBack();
                }
            });


        }else{
            final tempLostRR rr = rrList.get(position-1);
            if (rr.getJpgStr()!=null&&!TextUtils.isEmpty(rr.getJpgStr())){
                ((WriteHolder)holder).pictureList.setVisibility(View.VISIBLE);
                List<String> pic=new ArrayList<>();
                String[] jpg=rr.getJpgStr().split(",");
                //由于是本地加载，判断用户是否删除文件，如果删除这移出此项
                for (String aJpg : jpg) {
                    // TODO: 2017/7/6  1.上传时处理没有图片的问题，2.此处处理,没有图片的删除
                    File file = new File(aJpg);
                    if (file.exists()) {
                        pic.add(aJpg);
                    }
                }
                if (pic.size()<=0){
                    ((WriteHolder)holder).pictureList.setVisibility(View.GONE);
                }
                ImgGradeAdapter adapter=new ImgGradeAdapter(mContext,pic);
                ((WriteHolder)holder).pictureList.setAdapter(adapter);
                ((WriteHolder)holder).pictureList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        //      String path= (String) parent.getItemAtPosition(position);
                        mContext.startActivity(new Intent(mContext, ImageViewPagerActivity.class)
                                .putExtra("filepath",rr.getJpgStr()).putExtra("position",position));
                    }
                });

            }else{
                ((WriteHolder)holder).pictureList.setVisibility(View.GONE);
            }
            if (rr.getVideoStr()!=null&&!TextUtils.isEmpty(rr.getVideoStr())){
                ((WriteHolder)holder).videoList.setVisibility(View.VISIBLE);
                String[] video=rr.getVideoStr().split(",");
                List<String> list=new ArrayList<>();
                for (String aVideo : video) {
                    File file = new File(aVideo);
                    if (file.exists()) {
                        list.add(aVideo);
                    }
                }
                //判断是否为空，空着不显示
                if (list.size()<=0){
                    ((WriteHolder)holder).videoList.setVisibility(View.GONE);
                }
                ImgGradeAdapter adapter=new ImgGradeAdapter(mContext,list);
                ((WriteHolder)holder).videoList.setAdapter(adapter);
                ((WriteHolder)holder).videoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String path= (String) parent.getItemAtPosition(position);
                        mContext.startActivity(new Intent(mContext,PlayVideoActiviy.class).putExtra("file_path",path).putExtra("code_play",2));
                        //视频播放，单个
                    }
                });
            }else{
                ((WriteHolder)holder).videoList.setVisibility(View.GONE);
            }
            ((WriteHolder)holder).EvaTitle.setText(rr.getRowName());
            ((WriteHolder)holder).taskScore.setText("评分："+rr.getScore());
            ((WriteHolder)holder).taskScore.setTextColor(ContextCompat.getColor(mContext,R.color.delayTask));
            if(rr.getDescStr()==null|| TextUtils.isEmpty(rr.getDescStr())){
                ((WriteHolder)holder).textContent.setVisibility(View.GONE);
            }else {
                ((WriteHolder)holder).textContent.setText("描述："+rr.getDescStr());
            }
            ((WriteHolder)holder).modifiEva.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tempLostRR rr=rrList.get(position-1);
                    mbtnOnClickLisener.modifiClick(rr);
                }
            });
            ((WriteHolder)holder).deleteEva.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   mbtnOnClickLisener.deleteClick(position-1);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return rrList!=null?rrList.size()+1:1;
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        TextView jibendata;
        TextView evaDesc;
        TextView signature;
        TextView rectRequest;
        TextView addEva;
        public HeadHolder(View itemView) {
            super(itemView);
            jibendata = (TextView) itemView.findViewById(R.id.jibenData);
            evaDesc = (TextView) itemView.findViewById(R.id.evaDesc);
            signature = (TextView) itemView.findViewById(R.id.signature);
            rectRequest = (TextView) itemView.findViewById(R.id.rectRequest);
            addEva = (TextView) itemView.findViewById(R.id.addEva);
        }
    }

    class WriteHolder extends RecyclerView.ViewHolder {
        TextView EvaTitle;
        MyScrollGridView pictureList;
        MyScrollGridView videoList;
        TextView textContent;
        TextView modifiEva;
        TextView deleteEva;
        TextView taskScore;
        private WriteHolder(View itemView) {
            super(itemView);
            EvaTitle= (TextView) itemView.findViewById(R.id.EvaTitle);
            textContent= (TextView) itemView.findViewById(R.id.textContent);
            modifiEva= (TextView) itemView.findViewById(R.id.modifiEva);
            deleteEva= (TextView) itemView.findViewById(R.id.deleteEva);
            pictureList= (MyScrollGridView) itemView.findViewById(R.id.pictureList);
            videoList= (MyScrollGridView) itemView.findViewById(R.id.videoList);
            taskScore= (TextView) itemView.findViewById(R.id.taskScore);
        }
    }

    public interface BtnOnClickLisener{
        void modifiClick(tempLostRR bean);
        void deleteClick(int position);
        void addEvaClick();
        void onDesc();
        void goSignature();
        void feedBack();
        void jibenClick();
    }
}
